/*
 * @Author: Yuchen Wang
 * @Date: 2025-05-19 02:38:14
 * @LastEditors: Yuchen Wang
 * @LastEditTime: 2025-05-19 02:38:15
 * @FilePath: /react-pixel-progress/src/lib/utils/color.ts
 * @Description:
 * Copyright (c) 2025 by Yuchen Wang, All Rights Reserved.
 */

/**
 * 将 HEX 颜色值转换为 RGB 格式
 * @param hex HEX 颜色值，例如 '#ffffff' 或 'fff'
 * @returns 返回 RGB 格式的颜色值，例如 'rgb(255, 255, 255)'
 */
const hexToRgb = (hex: string) => {
  const hexWithoutHash = hex.replace(/^#/, '');
  const expandedHex =
    hexWithoutHash.length === 3
      ? hexWithoutHash
          .split('')
          .map((c) => c + c)
          .join('')
      : hexWithoutHash;
  const r = parseInt(expandedHex.substring(0, 2), 16);
  const g = parseInt(expandedHex.substring(2, 4), 16);
  const b = parseInt(expandedHex.substring(4, 6), 16);
  return `rgb(${r}, ${g}, ${b})`;
};

export default hexToRgb;
